package com.thedazzler.droidicon.badges;

import com.thedazzler.droidicon.factory.PixelMapFactory;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

public abstract class BaseDroidiconBadge extends Component implements Component.DrawTask {

    private static final String ATTRIBUTE_ICON_COLOR = "attr_icon_color";

    private static final String ATTRIBUTE_BG_COLOR = "attr_bg_color";

    private static final String ATTRIBUTE_PADDING = "attr_padding";

    private static final String ATTRIBUTE_IMG_SRC = "attr_img_src";

    private static final String ATTRIBUTE_RADIUS = "attr_radius";

    private static final String ATTRIBUTE_IS_CIRCULAR = "attr_circular";

    /**图片资源*/
    protected int mResId;
    /**icon 颜色*/
    protected int mIconColor;
    /**背景颜色*/
    protected int mBackgroundColor;
    /**组件圆角半径*/
    protected int mRadius;
    /**组件 padding*/
    protected int mPadding;
    /**组件宽高*/
    protected int mWidth;
    /**组件高度*/
    protected int mHeight;
    /**icon 宽高*/
    protected int lengthOfSide_icon;

    protected boolean isCircular = false;

    private Paint mPaint;

    public BaseDroidiconBadge(Context context) {
        super(context);
        init(null);
    }

    public BaseDroidiconBadge(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(attrSet);
    }

    public BaseDroidiconBadge(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(attrSet);
    }

    public BaseDroidiconBadge(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init(attrSet);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mResId==0)return;
        if (mWidth==0||mHeight==0){
            mWidth = getWidth();
            mHeight = getHeight();
        }
        if (mWidth != mHeight){
            mWidth = Math.min(mWidth , mHeight);
            mHeight = mWidth;
        }
        if (mPadding==0){
            mPadding = getPadding()[0];
        }
        lengthOfSide_icon = mWidth-2*mPadding;
        mRadius = isCircular() ? mWidth/2 : Math.min(mWidth/2 , mRadius);
        Path path = new Path();
        path.addRoundRect(new RectFloat(0, 0, mWidth, mHeight), mRadius, mRadius, Path.Direction.CLOCK_WISE);
        canvas.clipPath(path, Canvas.ClipOp.INTERSECT);
        canvas.drawColor(mBackgroundColor, Canvas.PorterDuffMode.SRC);
        PixelMapHolder mapHolder = new PixelMapHolder(PixelMapFactory.CreatePixelMap(getContext(), mResId, mIconColor, lengthOfSide_icon, lengthOfSide_icon));
        canvas.drawPixelMapHolder(mapHolder, mPadding,mPadding, mPaint);
    }

    private void init(AttrSet attrSet){
        if (attrSet!=null){
            boolean present = attrSet.getAttr(ATTRIBUTE_IMG_SRC).isPresent();
            mResId = present ? toIntegerValue(attrSet.getAttr(ATTRIBUTE_IMG_SRC).get().getStringValue()) : resId();
            present = attrSet.getAttr(ATTRIBUTE_ICON_COLOR).isPresent();
            mIconColor = present ? attrSet.getAttr(ATTRIBUTE_ICON_COLOR).get().getColorValue().getValue() : iconColor()==0 ? 0xFFFFFFFF : iconColor();
            present = attrSet.getAttr(ATTRIBUTE_BG_COLOR).isPresent();
            mBackgroundColor = present ? attrSet.getAttr(ATTRIBUTE_BG_COLOR).get().getColorValue().getValue() : backgroundColor();
            present = attrSet.getAttr(ATTRIBUTE_RADIUS).isPresent();
            mRadius = present ? attrSet.getAttr(ATTRIBUTE_RADIUS).get().getDimensionValue() : radius();
            present = attrSet.getAttr(ATTRIBUTE_IS_CIRCULAR).isPresent();
            isCircular = present ? attrSet.getAttr(ATTRIBUTE_IS_CIRCULAR).get().getBoolValue() : isCircular();
            present = attrSet.getAttr(ATTRIBUTE_PADDING).isPresent();
            mPadding = present ? attrSet.getAttr(ATTRIBUTE_PADDING).get().getDimensionValue() : padding();
        }else {
            mResId = resId();
            mIconColor = iconColor()==0 ? 0xFFFFFFFF : iconColor();
            mBackgroundColor = backgroundColor();
            mRadius = radius();
            mPadding = padding();
        }
        mWidth = width();
        mHeight = height();
        isCircular = isCircular();
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        addDrawTask(this);
    }

    private int toIntegerValue(String res){
        try {
            if (res==null)return 0;
            int index = res.indexOf(":")+1;
            String value = res.substring(index);
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * resource/base/media 下资源图片不少，如果可以请选择保留你需要的资源即可
     * @return icon 图片资源
     */
    public abstract int resId();

    /**
     * icon color 设置
     * @return icon color
     */
    public abstract int iconColor();

    /**
     * badge 背景色设置
     * @return badge 背景色
     */
    public abstract int backgroundColor();

    /**
     * badge 圆角半径设置
     * @return badge 圆角半径
     */
    public abstract int radius();

    /**
     * padding 设置
     * @return padding
     */
    public abstract int padding();

    /**
     * 代码设置badge宽度，则xml中宽度设置无效
     * @return badge宽度
     */
    public abstract int width();

    /**
     * 代码设置badge高度，则xml中高度设置无效
     * @return badge高度
     */
    public abstract int height();

    /**
     * 是否是圆形badge
     * @return 是否
     */
    public boolean isCircular() {
        return isCircular;
    }

    /**
     * 设置圆形显示
     * @param circularBadge 是否圆形
     * @return this
     */
    public BaseDroidiconBadge showCircularBadge(boolean circularBadge) {
        isCircular = circularBadge;
        return this;
    }

    /**
     * 设置资源图片
     * @param mResId 资源 id
     * @return this
     */
    public BaseDroidiconBadge setResId(int mResId) {
        this.mResId = mResId;
        return this;
    }

    /**
     * 设置背景色
     * @param mBackgroundColor background color
     * @return this
     */
    public BaseDroidiconBadge setBackgroundColor(int mBackgroundColor) {
        this.mBackgroundColor = mBackgroundColor;
        return this;
    }

    /**
     * 设置 icon color
     * @param mIconColor icon color
     * @return this
     */
    public BaseDroidiconBadge setIconColor(int mIconColor) {
        this.mIconColor = mIconColor;
        return this;
    }

    /**
     * 设置 padding
     * @param padding padding
     * @return this
     */
    public BaseDroidiconBadge setIconPadding(int padding){
        this.mPadding = padding;
        return this;
    }

    /**
     * 设置圆角半径
     * @param radius 半径
     * @return this
     */
    public BaseDroidiconBadge setRadius(int radius){
        this.mRadius = radius;
        return this;
    }

    /**
     * 设置图标大小
     * @param size size
     * @return this
     */
    public BaseDroidiconBadge setSize(int size){
        this.mWidth = mHeight = size;
        return this;
    }
}
